/*
 * @Author: xiaosihan
 * @Date: 2023-03-05 17:31:25
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-30 15:01:36
 */

import { flatten, get, isNumber, round } from 'lodash';
import { autorun, toJS } from 'mobx';
import { Box3, BufferAttribute, BufferGeometry, DoubleSide, Group, Mesh, MeshBasicMaterial, PlaneGeometry, Shape, ShapeGeometry, Vector3 } from 'three';
import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js';
import { degToRad } from 'three/src/math/MathUtils';
import rockMapPNG from './rockMap.png';
import geometryJSON from './chengdu.json';
import chengdu_uvPNG from './chengdu_uv.png';
import ExtrudeMesh, { ExtrudeMeshProps } from 'three-base/ExtrudeMesh';
import threeLoader from 'three-base/threeLoader';
import threeUtils from 'three-base/threeUtils';
import keyContorls from 'three-base/keyContorls';

import { Line2 } from 'three/examples/jsm/lines/Line2.js';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry.js';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial.js';
import AreaName1 from '../AreaName1/AreaName1';
import AreaName3 from '../AreaName3/AreaName3';
import Ball from '../Ball/Ball';
import RingSprite from '../RingSprite/RingSprite';
import chengdu_mapPNG from '../SiChuanMap/chengdu_map.png';
import axios from 'axios';
import chengduMap3DStore from '../chengduMap3DStore';
import doubtfulSituationStore from '@views/DoubtfulSituation/doubtfulSituationStore';
import largeScreenApi from '@api/largeScreenApi';

// 成都地图
export default class ChengDuMap extends Group {
  constructor() {
    super();
  }

  //创建区模型对象
  creatAreaMesh = (() => {
    // 区模型的纹理
    const material = new MeshBasicMaterial({
      color: '#47b1ff',
      map: threeLoader.getTexture(chengdu_mapPNG),
    });

    return (areaCode: number) => {
      const areaMesh = new Mesh<any, any>(new PlaneGeometry(1, 1, 1, 1), material);
      autorun(() => {
        areaMesh.visible = areaCode === chengduMap3DStore.areaCode;
      });
      this.add(areaMesh);
      return areaMesh;
    };
  })();

  //"name": "锦江区",510104,
  jingjiangMesh = this.creatAreaMesh(510104);
  //"name": "青羊区", 510105,
  qingyangMesh = this.creatAreaMesh(510105);
  //"name": "金牛区", 510106,
  jinniuMesh = this.creatAreaMesh(510106);
  //"name": "武侯区", 510107,
  wuhouMesh = this.creatAreaMesh(510107);
  //"name": "成华区", 510108,
  chenghuaMesh = this.creatAreaMesh(510108);
  //"name": "龙泉驿区", 510112,
  longquanyiMesh = this.creatAreaMesh(510112);
  //"name": "青白江区", 510113,
  qingbaijiangMesh = this.creatAreaMesh(510113);
  //"name": "新都区", 510114,
  xinduMesh = this.creatAreaMesh(510114);
  //"name": "温江区", 510115,
  wenjiangMesh = this.creatAreaMesh(510115);
  //"name": "双流区", 510116,
  shuangliuMesh = this.creatAreaMesh(510116);
  //"name": "郫都区", 510117,
  piduMesh = this.creatAreaMesh(510117);
  //"name": "金堂县", 510121,
  jintangMesh = this.creatAreaMesh(510121);
  //"name": "大邑县", 510129,
  dayiMesh = this.creatAreaMesh(510129);
  //"name": "蒲江县", 510131,
  pujiangMesh = this.creatAreaMesh(510131);
  //"name": "新津区", 510118,
  xinjinMesh = this.creatAreaMesh(510118);
  //"name": "都江堰市", 510181,
  dujiangyanMesh = this.creatAreaMesh(510181);
  //"name": "彭州市", 510182,
  penzhouMesh = this.creatAreaMesh(510182);
  //"name": "邛崃市", 510183,
  qionglaiMesh = this.creatAreaMesh(510183);
  //"name": "崇州市", 510184,
  chongzhouMesh = this.creatAreaMesh(510184);
  //"name": "简阳市", 510185,
  jianyanMesh = this.creatAreaMesh(510185);

  // 地图面模型材质
  sideMaterial = new MeshBasicMaterial({
    color: '#47b1ff',
    map: threeLoader.getTexture(chengdu_uvPNG),
    side: DoubleSide,
  });

  // 地图面模型
  sideGroup = (() => {
    const sideGroup = new (class SideGroup extends Group {})();

    geometryJSON.features.map((f, index) => {
      const { adcode, name } = f.properties;
      let { coordinates, type } = f.geometry;

      let points: number[][][] = [];
      if (type === 'MultiPolygon') {
      }

      if (!isNumber(get(coordinates, [0, 0, 0]))) {
        points.push(...flatten(coordinates));
      }

      let geometrys: Array<BufferGeometry> = [];

      for (let i = 0; i < points.length; i++) {
        let shape = new Shape();
        for (let ii = 0; ii < points[i].length; ii++) {
          const [x, y] = points[i][ii];
          if (ii === 0) {
            shape.moveTo(x, y);
          } else {
            shape.lineTo(x, y);
          }
        }

        geometrys.push(new ShapeGeometry(shape));
      }

      let geometry = BufferGeometryUtils.mergeGeometries(geometrys);

      geometry.rotateX(degToRad(-90));

      // 每一个区的模型对象
      const areaMesh = new Mesh(geometry, this.sideMaterial.clone());

      areaMesh.userData = { cursor: 'pointer', name, adcode };
      areaMesh.name = name;

      //区的点击事件
      areaMesh.addEventListener('click', (e) => {
        chengduMap3DStore.setAreaCode(adcode);
        // //@ts-ignore
        // const name = e.object.name;
        // //@ts-ignore
        // const p = (e.point as Vector3).toArray().map(v => round(v, 3)).join(",");
        // console.log(`{ name: "${name}", position: new Vector3(${p}) },`);
      });

      // 克隆一个区网格对象,并计算uv
      const areaGeo = areaMesh.geometry.clone();
      areaGeo.center();
      const size = threeUtils.getSize(areaGeo);
      const sideLength = Math.max(size.x, size.z);
      const position = areaGeo.getAttribute('position') as BufferAttribute;
      const uv = areaGeo.getAttribute('uv') as BufferAttribute;
      for (let i = 0; i < position.count; i++) {
        uv.setX(i, (position.getX(i) + sideLength / 2) / sideLength);
        uv.setY(i, 1 - (position.getZ(i) + sideLength / 2) / sideLength);
      }
      areaGeo.setAttribute('uv', uv);

      // 锦江地图
      if ([510104].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.jingjiangMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.jingjiangMesh.position.copy(center);
          this.jingjiangMesh.position.setY(0.1);
        });

        //青羊区
      } else if ([510105].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.qingyangMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.qingyangMesh.position.copy(center);
          this.qingyangMesh.position.setY(0.1);
        });

        //金牛区 510106
      } else if ([510106].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.jinniuMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.jinniuMesh.position.copy(center);
          this.jinniuMesh.position.setY(0.1);
        });

        //武侯区", 510107,
      } else if ([510107].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.wuhouMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.wuhouMesh.position.copy(center);
          this.wuhouMesh.position.setY(0.1);
        });
        //成华区", 510108,
      } else if ([510108].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.chenghuaMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.chenghuaMesh.position.copy(center);
          this.chenghuaMesh.position.setY(0.1);
        });
        //龙泉驿区", 510112,
      } else if ([510112].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.longquanyiMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.longquanyiMesh.position.copy(center);
          this.longquanyiMesh.position.setY(0.1);
        });
        //青白江区", 510113,
      } else if ([510113].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.qingbaijiangMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.qingbaijiangMesh.position.copy(center);
          this.qingbaijiangMesh.position.setY(0.1);
        });
        //新都区", 510114,
      } else if ([510114].includes(adcode)) {
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.xinduMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.xinduMesh.position.copy(center);
          this.xinduMesh.position.setY(0.1);
        });
        //温江区", 510115,
      } else if ([510115].includes(adcode)) {
        // wenjiangMesh = this.creatAreaMesh(510115);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.wenjiangMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.wenjiangMesh.position.copy(center);
          this.wenjiangMesh.position.setY(0.1);
        });
        //双流区", 510116,
      } else if ([510116].includes(adcode)) {
        // shuangliuMesh = this.creatAreaMesh(510116);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.shuangliuMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.shuangliuMesh.position.copy(center);
          this.shuangliuMesh.position.setY(0.1);
        });
        //郫都区", 510117,
      } else if ([510117].includes(adcode)) {
        // piduMesh = this.creatAreaMesh(510117);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.piduMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.piduMesh.position.copy(center);
          this.piduMesh.position.setY(0.1);
        });
        //金堂县", 510121,
      } else if ([510121].includes(adcode)) {
        // jintangMesh = this.creatAreaMesh(510121);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.jintangMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.jintangMesh.position.copy(center);
          this.jintangMesh.position.setY(0.1);
        });
        //大邑县", 510129,
      } else if ([510129].includes(adcode)) {
        // dayiMesh = this.creatAreaMesh(510129);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.dayiMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.dayiMesh.position.copy(center);
          this.dayiMesh.position.setY(0.1);
        });
        //蒲江县", 510131,
      } else if ([510131].includes(adcode)) {
        // pujiangMesh = this.creatAreaMesh(510131);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.pujiangMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.pujiangMesh.position.copy(center);
          this.pujiangMesh.position.setY(0.1);
        });
        //新津区", 510132,
      } else if ([510118].includes(adcode)) {
        // xinjinMesh = this.creatAreaMesh(510132);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.xinjinMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.xinjinMesh.position.copy(center);
          this.xinjinMesh.position.setY(0.1);
        });
        //都江堰市", 510181,
      } else if ([510181].includes(adcode)) {
        // dujiangyanMesh = this.creatAreaMesh(510181);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.dujiangyanMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.dujiangyanMesh.position.copy(center);
          this.dujiangyanMesh.position.setY(0.1);
        });
        //彭州市", 510182,
      } else if ([510182].includes(adcode)) {
        // penzhouMesh = this.creatAreaMesh(510182);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.penzhouMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.penzhouMesh.position.copy(center);
          this.penzhouMesh.position.setY(0.1);
        });
        //邛崃市", 510183,
      } else if ([510183].includes(adcode)) {
        // qionglaiMesh = this.creatAreaMesh(510183);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.qionglaiMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.qionglaiMesh.position.copy(center);
          this.qionglaiMesh.position.setY(0.1);
        });
        //崇州市", 510184,
      } else if ([510184].includes(adcode)) {
        // chongzhouMesh = this.creatAreaMesh(510184);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.chongzhouMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.chongzhouMesh.position.copy(center);
          this.chongzhouMesh.position.setY(0.1);
        });
        //简阳市", 510185,
      } else if ([510185].includes(adcode)) {
        // jianyanMesh = this.creatAreaMesh(510185);
        areaMesh.addEventListener('click', (e) => {
          //请求锦江的机构数据
          axios.get('./intelligent_check/map_data_liangshan.json').then((res) => {
            const map_data = res.data;
            doubtfulSituationStore.setMap_data(map_data);
            doubtfulSituationStore.setActiveMap(map_data[0]);
          });
        });
        this.jianyanMesh.geometry = areaGeo;
        threeUtils.addWork(() => {
          const center = threeUtils.getCenter(areaMesh);
          this.jianyanMesh.position.copy(center);
          this.jianyanMesh.position.setY(0.1);
        });
      }

      // areaMesh.addEventListener("mouseenter", e => {
      //     homeStore.setHoverAreaName(f.properties.name);
      //     homeStore.setHoverAreaOpacity(1);
      // });
      // areaMesh.addEventListener("mouseleave", e => {
      //     homeStore.setHoverAreaName("");
      //     homeStore.setHoverAreaOpacity(0);
      // });
      // areaMesh.addEventListener("mousemove", e => {
      //     homeStore.setHoverAreaPosition(e.x, e.y);
      // });

      // areaMesh.addEventListener("click", e => {
      //     console.log(e);
      // });

      sideGroup.add(areaMesh);
    });

    // 计算每个模型的uv的值, 并移动到中心点
    const box3 = new Box3();
    const center = new Vector3();
    const size = new Vector3();
    box3.setFromObject(sideGroup);
    box3.getSize(size);
    box3.getCenter(center);
    const sideLength = Math.max(size.x, size.z);

    sideGroup.traverse((obj) => {
      let mesh = obj as Mesh;
      if (mesh.isMesh) {
        mesh.geometry.translate(-center.x, -center.y, -center.z);

        const position = mesh.geometry.getAttribute('position') as BufferAttribute;
        const uv = mesh.geometry.getAttribute('uv') as BufferAttribute;

        for (let i = 0; i < position.count; i++) {
          uv.setX(i, (position.getX(i) + sideLength / 2) / sideLength);
          uv.setY(i, 1 - (position.getZ(i) + sideLength / 2) / sideLength);
        }
        mesh.geometry.setAttribute('uv', uv);
      }
    });

    autorun(() => {
      const { areaCode } = chengduMap3DStore;
      const visible = [510100, 510199, 510140, 510141, 510142].includes(areaCode);
      sideGroup.visible = visible;
    });

    // threeUtils.scaleTo(sideGroup, 10);
    threeUtils.centerObject3D(sideGroup);
    sideGroup.position.set(0, 0.1, 0);

    this.add(sideGroup);
    return sideGroup;
  })();

  // 岩石层材质
  rockMaterial = new MeshBasicMaterial({
    color: '#fff',
    side: DoubleSide,
    map: threeLoader.getTexture(rockMapPNG),
  });

  //每块地图独立添加岩石层
  rockGroup = (() => {
    const rockGroup = new (class RockGroup extends Group {})();

    // 更具选中的样式
    autorun(() => {
      // const { areaCode } = I3401L01Store;
      // material.opacity = (areaCode === "370000") ? 1.0 : 0.5;
      // plane.constant = areaCode === "370000" ? 2 : -1;
      // rockGroup.children.map(mesh => {
      //     mesh.visible = ["370000", mesh.uuid].includes(areaCode);
      // });
    });

    // 岩层成挤出路径
    const rockMeshoptions: ExtrudeMeshProps = {
      paths: [
        // { vector3: [-304, 0.1, 250], radius: 1, segments: 1, scale: { x: 1, y: 1 }, name: "" },
      ],
      shapePath: [
        [0, -1],
        [0, 1],
      ],
      closed: true,
    };

    // 提取每个区的地图轮廓
    geometryJSON.features.map((f, index) => {
      const { adcode, name } = f.properties;
      let { coordinates, type } = f.geometry;

      let points: number[][][] = [];
      if (type === 'MultiPolygon') {
      }

      if (!isNumber(get(coordinates, [0, 0, 0]))) {
        points.push(...flatten(coordinates));
      }

      for (let i = 0; i < points.length; i++) {
        rockMeshoptions.paths = [];
        if (points[i].length > 20) {
          for (let ii = 0; ii < points[i].length; ii++) {
            const [x, y] = points[i][ii];
            rockMeshoptions.paths.push({
              vector3: [x, 0, y * -1],
              radius: 1,
              segments: 0,
              scale: { x: 0.1, y: 0.1 },
              name: '',
            });
          }
        }

        // 创建岩石层对象
        const rockMesh = new ExtrudeMesh(rockMeshoptions);
        rockMesh.material = this.rockMaterial;
        rockMesh.userData = { cursor: 'pointer', name, adcode };

        autorun(() => {
          const { areaCode } = chengduMap3DStore;
          const visible = [510100, adcode].includes(areaCode);
          rockMesh.visible = visible;
        });

        rockMesh.name = name;
        // const box3 = new Box3();
        // const center = new Vector3();
        // box3.setFromObject(rockMesh);
        // box3.getCenter(center);
        // rockMesh.geometry.translate(0, 0, 0);
        rockGroup.add(rockMesh);
      }
    });

    // threeUtils.scaleTo(rockGroup, 10);
    threeUtils.centerObject3D(rockGroup);

    autorun(() => {
      const { areaCode } = chengduMap3DStore;
      const visible = [510100, 510199, 510140, 510141, 510142].includes(areaCode);
      rockGroup.visible = visible;
    });

    this.add(rockGroup);

    // rockGroup.position.set(0, 0, 0);
    // rockGroup.scale.set(1, 1, 1);
    return rockGroup;
  })();

  // 线材质
  lineMaterial = new LineMaterial({
    color: 0x84e0f6,
    linewidth: 1,
    // linewidth: 0.002,
    // worldUnits: true,
    vertexColors: false,
    alphaToCoverage: false,
  });

  // 地图的边界线
  mapLine = (() => {
    const mapLine = new Group();

    geometryJSON.features.map((f, index) => {
      const { adcode, name } = f.properties;
      let { coordinates, type } = f.geometry;
      let points = f.geometry.coordinates;
      // let points: number[][][] = [];
      if (type === 'MultiPolygon') {
      }

      if (!isNumber(get(coordinates, [0, 0, 0]))) {
        //@ts-ignore
        points.push(...flatten(coordinates));
      }

      for (let i = 0; i < points.length; i++) {
        const lineGeometry = new LineGeometry();
        const positions = [];
        for (let ii = 0; ii < points[i].length; ii++) {
          const [x, y] = points[i][ii];
          //@ts-ignore
          positions.push(x, 0, y * -1);
        }
        //@ts-ignore
        lineGeometry.setPositions(positions);

        const line2 = new Line2(lineGeometry, this.lineMaterial);
        line2.name = name;

        autorun(() => {
          const { areaCode } = chengduMap3DStore;
          const visible = [510100, adcode].includes(areaCode);
          line2.visible = visible;
        });

        mapLine.add(line2);
      }
    });

    // 计算每个模型的uv的值, 并移动到中心点
    const box3 = new Box3();
    const center = new Vector3();
    box3.setFromObject(mapLine);
    box3.getCenter(center);
    mapLine.traverse((line) => {
      if ((line as Line2).isLine2) {
        (line as Line2).geometry.translate(-center.x, -center.y, -center.z);
      }
    });

    autorun(() => {
      // const { areaCode } = chengduMap3DStore;
      // const visible = [510100,510199,510140,510141,510142].includes(areaCode);
      // mapLine.visible = visible;
    });

    mapLine.position.set(0, 0.102, 0);
    this.add(mapLine);

    return mapLine;
  })();

  // // 地区标签的模型组
  areaLableGroup = (() => {
    const areaLableGroup = new (class AreaLableGroup extends Group {})();

    const data = [
      { areaName: '彭州市', areaCode: 510182, iconColor: '', position: new Vector3(-0.066, 0.1, -0.325) },
      { areaName: '都江堰市', adcode: 510181, iconColor: '', position: new Vector3(-0.294, 0.1, -0.225) },
      { areaName: '简阳市', adcode: 510185, iconColor: '', position: new Vector3(0.611, 0.1, 0.396) },
      { areaName: '金堂县', adcode: 510121, iconColor: '', position: new Vector3(0.648, 0.1, 0.038) },
      { areaName: '邛崃市', adcode: 510183, iconColor: '', position: new Vector3(-0.701, 0.1, 0.387) },
      { areaName: '蒲江县', adcode: 510131, iconColor: '', position: new Vector3(-0.495, 0.1, 0.546) },
      { areaName: '新津区', adcode: 510118, iconColor: '', position: new Vector3(-0.119, 0.1, 0.367) },
      { areaName: '双流区', adcode: 510116, iconColor: '', position: new Vector3(0.108, 0.1, 0.376) },
      { areaName: '大邑县', adcode: 510129, iconColor: '', position: new Vector3(-0.639, 0.1, 0.151) },
      { areaName: '崇州市', adcode: 510184, iconColor: '', position: new Vector3(-0.282, 0.1, 0.135) },
      { areaName: '龙泉驿区', adcode: 510112, iconColor: '', position: new Vector3(0.351, 0.1, 0.176) },
      { areaName: '成华区', adcode: 510108, iconColor: '', position: new Vector3(0.189, 0.1, 0.078) },
      { areaName: '武侯区', adcode: 510107, iconColor: '', position: new Vector3(0.087, 0.1, 0.156) },
      { areaName: '温江区', adcode: 510115, iconColor: '', position: new Vector3(-0.116, 0.1, 0.06) },
      { areaName: '青羊区', adcode: 510105, iconColor: '', position: new Vector3(0.0122, 0.1, 0.079) },
      { areaName: '金牛区', adcode: 510106, iconColor: '', position: new Vector3(0.099, 0.1, 0.063) },
      { areaName: '青白江区', adcode: 510113, iconColor: '', position: new Vector3(0.414, 0.1, 0.001) },
      { areaName: '新都区', adcode: 510114, iconColor: '', position: new Vector3(0.217, 0.1, -0.038) },
      { areaName: '郫都区', adcode: 510117, iconColor: '', position: new Vector3(-0.069, 0.1, -0.06) },
      { areaName: '锦江区', adcode: 510104, iconColor: '', position: new Vector3(0.173, 0.1, 0.178) },
      //4个单独的科技区
      { areaName: '成都市本级', adcode: 510199, iconColor: '', position: new Vector3(0.5, 0.1, -0.5) },
      { areaName: '高新区', adcode: 510140, iconColor: '', position: new Vector3(0.75, 0.1, -0.5) },
      { areaName: '天府新区', adcode: 510141, iconColor: '', position: new Vector3(0.48, 0.1, -0.3) },
      { areaName: '东部新区', adcode: 510142, iconColor: '', position: new Vector3(0.73, 0.1, -0.3) },
    ];

    data.map((l) => {
      const areaName = l.iconColor ? new AreaName3(l.areaName, 16, '#fff', l.iconColor as 'red' | 'orange' | 'blue') : new AreaName1(l.areaName, 16, '#fff');
      areaName.position.copy(l.position);
      autorun(() => {
        const { areaCode } = chengduMap3DStore;
        const visible = [510100, l.adcode].includes(areaCode);
        areaName.visible = visible;
      });
      //@ts-ignore
      areaName.addEventListener('click', (e) => {
        chengduMap3DStore.setAreaCode(l.adcode);
      });
      areaLableGroup.add(areaName);
    });

    autorun(() => {
      // const { areaCode } = chengduMap3DStore;
      // const visible = [510100,510199,510140,510141,510142].includes(areaCode);
      // areaLableGroup.visible = visible;
    });

    this.add(areaLableGroup);
    return areaLableGroup;
  })();

  // 地区统计标签
  areaStatisticss = (() => {
    const areaStatisticss = new Group();

    const lablePositions = [
      { name: '邛崃市', position: new Vector3(-0.482, 0.1, 0.354) },
      { name: '大邑县', position: new Vector3(-0.544, 0.1, 0.13) },
      { name: '简阳市', position: new Vector3(0.49, 0.1, 0.482) },
      { name: '双流区', position: new Vector3(0.18, 0.1, 0.424) },
      { name: '新津区', position: new Vector3(-0.138, 0.1, 0.376) },
      { name: '温江区', position: new Vector3(-0.131, 0.1, 0.082) },
      { name: '崇州市', position: new Vector3(-0.216, 0.1, 0.157) },
      { name: '都江堰市', position: new Vector3(-0.354, 0.1, -0.141) },
      { name: '彭州市', position: new Vector3(-0.001, 0.1, -0.272) },
      { name: '金堂县', position: new Vector3(0.75, 0.1, 0.093) },
      { name: '龙泉驿区', position: new Vector3(0.328, 0.1, 0.221) },
      { name: '新都区', position: new Vector3(0.081, 0.1, -0.125) },
      { areaName: '彭州市', areaCode: 510182, iconColor: '', position: new Vector3(-0.066, 0.1, -0.325) },
      { areaName: '都江堰市', adcode: 510181, iconColor: '', position: new Vector3(-0.294, 0.1, -0.225) },
      { areaName: '简阳市', adcode: 510185, iconColor: '', position: new Vector3(0.611, 0.1, 0.396) },
      { areaName: '金堂县', adcode: 510121, iconColor: '', position: new Vector3(0.648, 0.1, 0.038) },
      { areaName: '邛崃市', adcode: 510183, iconColor: '', position: new Vector3(-0.701, 0.1, 0.387) },
      { areaName: '蒲江县', adcode: 510131, iconColor: '', position: new Vector3(-0.495, 0.1, 0.546) },
      { areaName: '新津区', adcode: 510118, iconColor: '', position: new Vector3(-0.119, 0.1, 0.367) },
      { areaName: '双流区', adcode: 510116, iconColor: '', position: new Vector3(0.108, 0.1, 0.376) },
      { areaName: '大邑县', adcode: 510129, iconColor: '', position: new Vector3(-0.639, 0.1, 0.151) },
      { areaName: '崇州市', adcode: 510184, iconColor: '', position: new Vector3(-0.282, 0.1, 0.135) },
      { areaName: '龙泉驿区', adcode: 510112, iconColor: '', position: new Vector3(0.351, 0.1, 0.176) },
      { areaName: '成华区', adcode: 510108, iconColor: '', position: new Vector3(0.189, 0.1, 0.078) },
      { areaName: '武侯区', adcode: 510107, iconColor: '', position: new Vector3(0.087, 0.1, 0.156) },
      { areaName: '温江区', adcode: 510115, iconColor: '', position: new Vector3(-0.116, 0.1, 0.06) },
      { areaName: '青羊区', adcode: 510105, iconColor: '', position: new Vector3(0.0122, 0.1, 0.079) },
      { areaName: '金牛区', adcode: 510106, iconColor: '', position: new Vector3(0.099, 0.1, 0.063) },
      { areaName: '青白江区', adcode: 510113, iconColor: '', position: new Vector3(0.414, 0.1, 0.001) },
      { areaName: '新都区', adcode: 510114, iconColor: '', position: new Vector3(0.217, 0.1, -0.038) },
      { areaName: '郫都区', adcode: 510117, iconColor: '', position: new Vector3(-0.069, 0.1, -0.06) },
      { areaName: '锦江区', adcode: 510104, iconColor: '', position: new Vector3(0.173, 0.1, 0.178) },

      //4个单独的科技区
      { areaName: '成都市本级', adcode: 510199, iconColor: '', position: new Vector3(0.5, 0.1, -0.47) },
      { areaName: '高新区', adcode: 510140, iconColor: '', position: new Vector3(0.75, 0.1, -0.47) },
      { areaName: '天府新区', adcode: 510141, iconColor: '', position: new Vector3(0.48, 0.1, -0.27) },
      { areaName: '东部新区', adcode: 510142, iconColor: '', position: new Vector3(0.73, 0.1, -0.27) },
    ];

    autorun(async () => {
      const { mapActive } = doubtfulSituationStore;
      let key = '';
      switch (mapActive) {
        case 0:
          key = '医疗机构分析情况';
          break;
        case 1:
          key = '药店分析情况';
          break;
        case 2:
          key = '护理机构分析情况';
          break;
        case 3:
          key = '辅助器具机构分析情况';
          break;
      }

      //  const sichuanMapData = toJS(doubtfulSituationStore.sichuan_map_data);
      const res = (await largeScreenApi.clue_statistics(key)) as any;
      areaStatisticss.children.length = 0;
      
      for (let i in res.list) {
        const { med_admdvs_name, total, rsk_cnt } = res.list[i];
        const lablePosition = lablePositions.find((l) => l.name === med_admdvs_name || l.areaName === med_admdvs_name);
        if (lablePosition) {
          const ringSprite = new RingSprite(total, rsk_cnt);
          ringSprite.position.copy(lablePosition.position);
          areaStatisticss.add(ringSprite);
        } else {
          // console.log(name);
        }
      }
    });

    autorun(() => {
      const { areaCode } = chengduMap3DStore;
      areaStatisticss.visible = 510100 === areaCode;
    });

    this.add(areaStatisticss);
    return areaStatisticss;
  })();

  //医院标签
  hospitalLables = (() => {
    const hospitalLables = new Group();

    // 成都机构的坐标位置
    const chengdu_points: Array<Vector3> = [
      new Vector3(0.26, 0.5, -0.547),
      new Vector3(0.422, 0.5, -0.403),
      new Vector3(0.498, 0.5, -0.151),
      new Vector3(0.709, 0.5, -0.344),
      new Vector3(1.089, 0.5, -0.55),
      new Vector3(1.151, 0.5, -0.267),
      new Vector3(1.38, 0.5, -0.621),
      new Vector3(1.627, 0.5, -0.57),
      new Vector3(1.528, 0.5, -0.188),
      new Vector3(1.806, 0.5, -0.259),
    ];

    // 凉山机构的坐标位置
    const liangshan_points: Array<Vector3> = [
      new Vector3(-2.186, 0.521, 1.97),
      new Vector3(-1.71, 0.521, 2.696),
      new Vector3(-1.3, 0.521, 2.081),
      new Vector3(-0.65, 0.521, 1.658),
      new Vector3(-0.089, 0.521, 1.642),
      new Vector3(-0.473, 0.52, 2.555),
      new Vector3(0.303, 0.521, 2.377),
      new Vector3(-0.699, 0.521, 3.66),
      new Vector3(-0.131, 0.521, 3.616),
    ];

    // 阿坝机构的坐标位置
    const aba_points: Array<Vector3> = [
      new Vector3(-1.96, 0.5, -2.188),
      new Vector3(-1.396, 0.5, -2.399),
      new Vector3(-0.672, 0.5, -2.784),
      new Vector3(0.213, 0.5, -2.921),
      new Vector3(0.996, 0.5, -3.001),
      new Vector3(-1.411, 0.5, -1.593),
      new Vector3(-0.751, 0.5, -1.773),
      new Vector3(-0.086, 0.5, -2.025),
      new Vector3(0.681, 0.5, -2.294),
      new Vector3(0.018, 0.5, -1.351),
    ];

    // 甘孜机构的坐标位置
    const gazi_points: Array<Vector3> = [
      new Vector3(-4.826, 0.5, -3.208),
      new Vector3(-4.345, 0.5, -2.079),
      new Vector3(-3.102, 0.5, -1.961),
      new Vector3(-2.289, 0.5, -1.329),
      new Vector3(-3.853, 0.5, -0.764),
      new Vector3(-2.767, 0.5, -0.07),
      new Vector3(-1.667, 0.5, -0.083),
      new Vector3(-2.963, 0.5, 0.683),
      new Vector3(-1.471, 0.5, 0.58),
      new Vector3(-1.321, 0.5, 1.276),
    ];

    autorun(() => {
      const { areaCode } = chengduMap3DStore;
      const mapData = toJS(doubtfulSituationStore.map_data);
      hospitalLables.children.length = 0;

      mapData.map((d: any, i: number) => {
        const areaName = new Ball(d.name.replace(/([(|(])/, '\n$1'));
        areaName.name = d.name;
        areaName.addEventListener('click', () => {
          doubtfulSituationStore.setActiveMap(d);
        });

        switch (areaCode) {
          // 成都视角
          case 510100:
            areaName.position.copy(chengdu_points[i % chengdu_points.length]);
            break;
          // 凉山视角
          case 513400:
            areaName.position.copy(liangshan_points[i % liangshan_points.length]);
            break;
          // 甘孜视角
          case 513300:
            areaName.position.copy(gazi_points[i % gazi_points.length]);
            break;
          // 阿坝视角
          case 513200:
            areaName.position.copy(aba_points[i % aba_points.length]);
            break;

          default:
            break;
        }

        hospitalLables.add(areaName);
      });
    });

    autorun(() => {
      const { activeMapData } = doubtfulSituationStore;
      hospitalLables.children.map((o) => {
        (o as Ball).isActive(o.name === activeMapData.name);
      });
    });

    autorun(() => {
      const { areaCode } = chengduMap3DStore;
      hospitalLables.visible = 510100 !== areaCode;
    });

    this.add(hospitalLables);
    return hospitalLables;
  })();
}
